package com.controller;

//会员登陆

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.constant.MessageConstant;
import com.constant.RedisMessageConstant;
import com.entity.Result;
import com.pojo.Member;
import com.qiniu.util.Json;
import com.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    //会员登陆
    @RequestMapping("/login")
    public Result login(HttpServletResponse response, @RequestBody Map map){
        String telephone = (String) map.get("telephone");
        //校验验证码  获取用户输入的验证码
        String validateCode = (String) map.get("validateCode");
        //获取redis中的验证码
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        String s1 = jedisPool.getResource().get(telephone + "lock_24h");
        if (s1 != null){
            return new Result(false, "验证码错误输入超过五次，请3分钟后再次登录");
        }

        String s = jedisPool.getResource().get(telephone + "error_count");
        int error_count = 0;
        if (s != null){
            error_count = Integer.parseInt(s);
        }
        if (error_count > 5){
            jedisPool.getResource().setex(telephone + "lock_24h",60*3,String.valueOf(error_count));
            return new Result(false, "验证码错误输入超过五次，请3分钟后再次登录");
        }
        if (validateCode != null && validateCodeInRedis !=null && validateCodeInRedis.equals(validateCode)){
            //验证通过
            jedisPool.getResource().del(telephone + "error_count");
            Member member = memberService.findBytelephone(telephone);
            if (member == null){
                //不是会员 自动注册
                member = new Member();
                member.setRegTime(new Date());
                member.setPhoneNumber(telephone);
                memberService.add(member);
            }
            //登陆成功 写入cookie 用于跟踪用户
            Cookie cookie = new Cookie("login_member_telephone",telephone);
            cookie.setPath("/");
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);
            //将会员信息保存到redis中
            String json = JSON.toJSON(member).toString();
            jedisPool.getResource().setex(telephone,60*30,json);
            return new Result(true,MessageConstant.LOGIN_SUCCESS);
        } else {
            //验证失败
            error_count++;
            jedisPool.getResource().set("error_count",String.valueOf(error_count));
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }
}
